package com.ld.security.server.admin.modules.ge.api;


import com.ld.security.common.core.util.JsonUtil;
import com.ld.security.common.mybatis.base.po.IdPo;
import com.ld.security.common.mybatis.base.po.IdsPo;
import com.ld.security.common.mybatis.base.po.PagePo;
import com.ld.security.common.mybatis.base.vo.DataVo;
import com.ld.security.common.mybatis.base.vo.PageVo;
import com.ld.security.common.tomcat.anno.AjaxLogKey;
import com.ld.security.common.tomcat.base.api.SuperApi;
import com.ld.security.common.tomcat.util.RespUtil;
import com.ld.security.server.admin.modules.ge.entity.GeViewEntity;
import com.ld.security.server.admin.modules.ge.po.UseTableInitViewPo;
import com.ld.security.server.admin.modules.ge.po.ViewSavePo;
import com.ld.security.server.admin.modules.ge.service.IGeViewService;
import com.ld.security.server.admin.modules.ge.vo.GeViewSaveVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@Api(tags="视图Api管理")
@RequestMapping("/api/geView")
public class GeViewApi extends  SuperApi<GeViewEntity> {

    @Autowired
    IGeViewService iGeViewService;

    @AjaxLogKey
    @ApiOperation("得到视图修改详情")
    @RequestMapping(value = "/getGeViewSave",method = RequestMethod.POST)
    public DataVo getGeViewSave(@Valid @RequestBody IdPo po){
        GeViewSaveVo vo = iGeViewService.getGeViewSave(po.getId());
        return DataVo.of(vo);
    }

    @AjaxLogKey
    @ApiOperation("保存视图")
    @RequestMapping(value = "/saveView",method = RequestMethod.POST)
    public DataVo saveView(@Valid @RequestBody ViewSavePo po){

       iGeViewService.saveView(po);
        return DataVo.ofMsg("保存成功");
    }

    @AjaxLogKey
    @ApiOperation("使用表初始化视图")
    @RequestMapping(value = "/useTableInitView",method = RequestMethod.POST)
    public DataVo UseTableInitView(@RequestBody UseTableInitViewPo po){



         iGeViewService.useTableInitView(po);
        return DataVo.ofMsg("初始化成功");
    }


    @ApiOperation("代码预览")
    @RequestMapping(value = "/generatingCode",method = RequestMethod.POST)
    public DataVo generatingCode(@RequestBody IdPo po){
         String code = iGeViewService.generatingCode(po.getId());
        return DataVo.of(code);
    }

    @AjaxLogKey

    @ApiOperation("代码下载")
    @RequestMapping(value = "/generatingCodeZip",method = RequestMethod.POST)

    public void generatingCodeZip(String json){
        IdsPo po =JsonUtil.toObject(json,IdsPo.class);

        byte[] bs = iGeViewService.generatingCodeZip(po);
        RespUtil.downloadFile("code.zip",bs);

    }

    @Override
    public DataVo<PageVo<GeViewEntity>> page(@RequestBody @Valid PagePo<GeViewEntity> po) {
        return dataPerPage(po);
    }

    @Override
    public DataVo<PageVo<GeViewEntity>> select(@RequestBody @Valid PagePo<GeViewEntity> po) {
        return dataPerPage(po);
    }

    @AjaxLogKey
    @RequestMapping(value = "/dataPerPage", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("数据权限查询数据")
    public DataVo<PageVo<GeViewEntity>> dataPerPage(@RequestBody @Valid PagePo<GeViewEntity> po) {

        PageVo<GeViewEntity> pageVo =  iGeViewService.dataPerPage(po);
        return DataVo.of(pageVo);
    }

}
